function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } var _ctN2W = { ones: ['zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'], tens: ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety'], scales: ['','thousand','million','billion','trillion','quadrillion'] }; function _ctN2WUnder1000(n) { n = Math.floor(n); var parts = []; var h = Math.floor(n / 100); var rem = n % 100; if (h > 0) parts.push(_ctN2W.ones[h] + ' hundred'); if (rem > 0) { if (rem < 20) parts.push(_ctN2W.ones[rem]); else { var t = Math.floor(rem / 10), o = rem % 10; parts.push(o > 0 ? (_ctN2W.tens[t] + '-' + _ctN2W.ones[o]) : _ctN2W.tens[t]); } } return parts.join(' '); } function _ctN2WIndian(intStr) { var out = []; // Split strings to preserve integers beyond JavaScript's safe integer range. if (intStr.length > 7) { out.push(_ctN2WIndian(intStr.slice(0, -7)) + ' crore'); intStr = intStr.slice(-7); } var n = parseInt(intStr, 10); var lakh = Math.floor(n / 100000); var thousand = Math.floor(n / 1000) % 100; if (lakh) out.push(_ctN2WUnder1000(lakh) + ' lakh'); if (thousand) out.push(_ctN2WUnder1000(thousand) + ' thousand'); if (n % 1000) out.push(_ctN2WUnder1000(n % 1000)); return out.join(' ') || 'zero'; } function _ctN2WIntStr(intStr) { if (!intStr || intStr === '0') return 'zero'; if (intStr.length > 18) return null; var system = document.getElementById('n2w-system'); if (system && system.value === 'indian') return _ctN2WIndian(intStr); var groups = []; var s = intStr; while (s.length > 0) { groups.unshift(s.slice(-3)); s = s.slice(0, -3); } if (groups.length > _ctN2W.scales.length) return null; var out = []; for (var i = 0; i < groups.length; i++) { var g = parseInt(groups[i], 10); if (!g) continue; var scale = _ctN2W.scales[groups.length - 1 - i]; var part = _ctN2WUnder1000(g); out.push(scale ? (part + ' ' + scale) : part); } return out.join(' '); } function _ctN2WMode() { var el = document.getElementById('n2w-mode'); return el ? el.value : 'standard'; } function _ctN2WCurrency() { var el = document.getElementById('n2w-currency'); return el ? el.value : 'dollars'; } function _ctN2WCurrencyNames(code) { switch (code) { case 'pounds': return { major: 'pound', majorPlural: 'pounds', minor: 'pence', singleMinor: 'penny' }; case 'euros': return { major: 'euro', majorPlural: 'euros', minor: 'cents', singleMinor: 'cent' }; default: return { major: 'dollar',majorPlural: 'dollars',minor: 'cents', singleMinor: 'cent' }; } } function convert(input) { var raw = (input || '').trim(); if (!raw) return ''; raw = raw.replace(/[, _]/g, '').replace(/[£€\$¥]/g, ''); var negative = false; if (raw.charAt(0) === '-') { negative = true; raw = raw.slice(1); } if (raw.charAt(0) === '+') raw = raw.slice(1); if (!/^\d+(\.\d+)?$/.test(raw)) return 'Please enter a valid number.'; var dot = raw.indexOf('.'); var intPart = dot === -1 ? raw : raw.slice(0, dot); var fracPart = dot === -1 ? '' : raw.slice(dot + 1); intPart = intPart.replace(/^0+/, '') || '0'; var mode = _ctN2WMode(); if (mode === 'cheque') { var names = _ctN2WCurrencyNames(_ctN2WCurrency()); var majorWords = _ctN2WIntStr(intPart); if (majorWords === null) return 'Number too large for cheque mode.'; var cents = '00'; if (fracPart) { cents = (fracPart + '00').slice(0, 2); } var majorLabel = (intPart === '1') ? names.major : names.majorPlural; var out = _ctTitleCaseFirst(majorWords) + ' ' + majorLabel + ' and ' + cents + '/100'; return (negative ? 'minus ' : '') + out; } var intWords = _ctN2WIntStr(intPart); if (intWords === null) return 'Number too large.'; var out2 = intWords; if (fracPart && /[^0]/.test(fracPart)) { var digits = []; for (var i = 0; i < fracPart.length; i++) { var d = parseInt(fracPart.charAt(i), 10); digits.push(_ctN2W.ones[d]); } out2 += ' point ' + digits.join(' '); } return (negative ? 'negative ' : '') + out2; } function _ctTitleCaseFirst(s) { if (!s) return s; return s.charAt(0).toUpperCase() + s.slice(1); } function _ctN2WDetectSystem() { try { var locale = navigator.languages && navigator.languages.length ? navigator.languages[0] : navigator.language; if (!locale) return 'international'; var groups = new Intl.NumberFormat(locale, { useGrouping: true }) .formatToParts(123456789).filter(function(part) { return part.type === 'integer'; }); return groups.length === 4 ? 'indian' : 'international'; } catch (e) { return 'international'; } } function _ctN2WInit() { var system = document.getElementById('n2w-system'); if (system) system.value = _ctN2WDetectSystem(); function updateCurrencyVisibility() { var cur = document.getElementById('n2w-currency-wrap'); if (cur) cur.style.display = (_ctN2WMode() === 'cheque') ? '' : 'none'; } ['n2w-mode', 'n2w-currency', 'n2w-system'].forEach(function(id) { var control = document.getElementById(id); if (control) control.addEventListener('change', function() { updateCurrencyVisibility(); var input = document.getElementById('box1'); if (input) input.dispatchEvent(new Event('keyup', { bubbles: true })); }); }); updateCurrencyVisibility(); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', _ctN2WInit); } else { _ctN2WInit(); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }